package com.example.demo.controller;

import com.example.demo.service.SingletonAndThreadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value="/singletonAndThread")
public class SingletonAndThreadController {

    @Autowired
    SingletonAndThreadService singletonAndThreadService;

    @RequestMapping(value="/test")
    public void test()
    {
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<100;i++)
        {
            list.add(i);
        }
        long startTime = System.currentTimeMillis();


//        list.parallelStream().forEach(e->{
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException interruptedException) {
//                interruptedException.printStackTrace();
//            }
//            System.out.println(e);
//        });

//        list.parallelStream().forEach(e->{
//            try {
//                singletonAndThreadService.show(e);
//            } catch (InterruptedException interruptedException) {
//                interruptedException.printStackTrace();
//            }
//        });
        list.stream().forEach(e->{
            try {
                singletonAndThreadService.show(e);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        });



        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");


    }
}
